Design by Contract: Not with lvalue subs

Mark Leighton Fisher on 2006-06-16T17:10:31

I'd thought that I would write about using Design by Contract preconditions and postconditions with Perl and VB. I had not used Perl lvalue subs before, but I thought it would be easy to use an lvalue sub to perform data validation as preconditions and postconditions. After much experimentation, then some Googling, I finally found a post about lvalue subs on Perl Monks explaining that you can't actually do anything with the value while you are in the lvalue sub:

  1. You can't examine the proposed new value before the assignment; and
  2. You can't check the value after assigning it.

Thus endeth the lesson for the day.


Contextual::Return

runrig on 2006-06-16T22:17:58

Contextual-Return claims to be able to accomplish this sort of validation.

Extending lvalue subs is possible

Limbic Region on 2006-06-17T15:02:59

You may find the following two links interesting

http://perlmonks.org/?node_id=424644

http://perlmonks.org/?node_id=369354

The latter explains how to use tied variables in conjunction with lvalue subs as one way to accomplish what you are doing.

There are modules on CPAN (referenced in the links I believe) that show better ways.